<?php
include('../fns_all.php');

function create_new_poll_question($question){
	$cxn = db_connect();
	$question = mysqli_real_escape_string($cxn, $question);
	$query = "INSERT INTO polls_questions (question)";
	$query .= "VALUES( ";
	$query .= "'" . $question. "')";
	return mysqli_query( $cxn, $query );
}

function insert_answers_for_question($question, $answers){
	$cxn = db_connect();
	$query = "SELECT * FROM polls_questions where question = '$question'";
	$result = mysqli_query($cxn, $query) or die('Error, get_single_user failed');
	$row = mysqli_fetch_assoc ($result);

	$question_id = $row["question_id"];
	foreach ($answers as $key => $value) {
        if ($value != '') {
        	$query = "INSERT INTO polls_answers (question_id, answer)";
			$query .= "VALUES( ";
			$query .= "'.$question_id.', ";
			$query .= "'" . $value. "')";
			mysqli_query( $cxn, $query );
        }
    }
}

function get_all_users_ordered_by_date(){
	$cxn = db_connect();
	$query = " SELECT * FROM users order by create_date desc ";
	$result = mysqli_query($cxn, $query) or die('Error, get_all_users_ordered_by_date failed');
	return $result;
}



function get_single_user($id){
	$cxn = db_connect();
	$query = " SELECT * FROM users where id='$id' order by create_date desc ";
	$result = mysqli_query($cxn, $query) or die('Error, get_single_user failed');
	return $result;
}

function change_user_rank($id, $rank){
	$cxn = db_connect();
	mysqli_query($cxn, "update users set rank='$rank' WHERE id='$id'");
}
?>
